package com.etoak.common.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RedisService {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    //=============验证码================
    //setex
    public  void setex(String key, String value, long expire, TimeUnit timeUnit){
        stringRedisTemplate.opsForValue().set(key,value,expire,timeUnit);
    }
    //get
    public String get(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    //=========购物车  hash==============
    public void hset(String key,String field,String value){
        stringRedisTemplate.opsForHash().put(key,field,value);
    }
    public List<String> hvals(String key){
        return stringRedisTemplate.opsForHash().values(key)
                .stream().map(Object::toString).toList();
    }
    public void hdel(String key,String... fields){
        stringRedisTemplate.opsForHash().delete(key,fields);
    }
}
